<?php

namespace app\common;

use think\Controller;
use think\response\Json;
/**
 * 基类控制器
 *
 * @author yyzh
 */
class BaseController extends Controller {
	public function _initialize() {
		if ($this->request->isOptions ()) {
			header ( 'Access-Control-Max-Age:2592000' );
			header ( 'Access-Control-Allow-Credentials:true' );
			header ( 'Access-Control-Allow-Origin:' . $this->request->header ( 'Origin', '*' ) );
			exit ();
		}
	}
	/**
	 * 以JSON输出
	 *
	 * @param unknown $data
	 * @param string $success 请求是否成功
	 * @param string $message 返回消息
	 * @return \think\response\Json
	 */
	protected function json($data) {
		$header = [ ];
		$header ['Access-Control-Max-Age'] = '2592000';
		$header ['Access-Control-Allow-Credentials'] = 'true';
		$header ['Access-Control-Allow-Origin'] = $this->request->header ( 'Origin', '*' );
		if ($data instanceof RenderJson) {
			return new Json ( $data->toArray (), 200, $header );
		} else {
			return new Json ( $data, 200, $header );
		}
	}
	/**
	 * 判断数据库查询返回是否为空
	 *
	 * @param unknown $result
	 */
	protected function isEmpty($result) {
		return (! is_array ( $result ) || count ( $result ) < 0);
	}
}
